package com.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mall.dao.*;
import com.mall.entity.Category;
import com.mall.entity.CategoryVO;
import com.mall.entity.ProductImg;
import com.mall.entity.ProductVO;
import com.mall.service.CategoryService;
import com.mall.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private CategoryVOMapper categoryVOMapper;
    @Autowired
    private ProductImgMapper productImgMapper;
    @Autowired
    private ProductVOMapper productVOMapper;

    //商品分类推荐
    @Override
    public ResultVO listFeaturedCategories() {
        //先查所有一级
        QueryWrapper<CategoryVO> categoryQuery = new QueryWrapper<>();

        categoryQuery.eq("category_level",1);
        List<CategoryVO> list = categoryVOMapper.selectList(categoryQuery);
        for (CategoryVO categoryVO :list) {
            List<ProductVO> productList = productVOMapper.selectBySoilNum();
            //把图片封装起来
            for (ProductVO productVO:productList){
                Integer categoryId = productVO.getCategoryId();
                QueryWrapper<ProductImg> imgsQuery = new QueryWrapper<>();
                imgsQuery.eq("item_id",categoryId);
                List<ProductImg> productImgList = productImgMapper.selectList(imgsQuery);
                productVO.setImgs(productImgList);
            }
            categoryVO.setProducts(productList);
        }
        return ResultVO.success(list);
    }

    @Override
    public List<CategoryVO> getCategories(Integer categoryLevel) {
        if (categoryLevel==1){
            return map().get(0);
        }
        if (categoryLevel==2){
            return ontList();
        }
        return towList();
    }
    //获取二级
    private List<CategoryVO> ontList(){
        Map<Integer, List<CategoryVO>> map = map();
        List<CategoryVO> ontList = map.get(0);
        for (CategoryVO OneCategory:ontList){
            Integer towParenId = OneCategory.getCategoryId();
            List<CategoryVO> list = map.get(towParenId);
            OneCategory.setCategories(list);
        }
        return ontList;
    }
    //获取三级
    private List<CategoryVO> towList(){
        List<CategoryVO> ontList = ontList();
        for (CategoryVO oneCategoryVO: ontList){
            List<CategoryVO> towList = oneCategoryVO.getCategories();
            if(towList==null || towList.size()==0){
                continue;
            }
            for (CategoryVO towCategoryVO:towList){
                Integer towParenId = towCategoryVO.getCategoryId();
                List<CategoryVO> list = map().get(towParenId);
                towCategoryVO.setCategories(list);
            }
        }
        return ontList;
    }

    private Map<Integer,List<CategoryVO>> map(){
        Map<Integer,List<CategoryVO>> map = new HashMap<>();
        List<CategoryVO> list = categoryVOMapper.selectList(null);
        for (CategoryVO categoryVO:list){
            Integer parentId = categoryVO.getParentId();
            if(map.containsKey(parentId)){
                map.get(parentId).add(categoryVO);
            }else{
                List<CategoryVO> list1 = new ArrayList<>();
                list1.add(categoryVO);
                map.put(parentId,list1);
            }
        }
        return map;
    }
}
